/**
 *  *2010-6-6 下午05:21:49  add 
 */
package com.base.myproject.client.tools;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.ui.Loading;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @see BaseStoreList
 * BaseStoreList里装载此类，此类是常用的store，
 * 通过定义name从服务器查找数据，name要与sqltable定义一致
 * desc是加载时候显示的信息
 * 默认传入bcode当前登录人员给服务器
 * @author Administrator
 *2010-6-6
 * javazgw@gmail.com
 */
public class BaseStoreCommand extends Command{

	String name; //服务器上设置的name一直
	String desc;
	BaseStore<BusiObject> store=new BaseStore<BusiObject>();
	public BaseStoreCommand(String name,String desc)
	{
		this.name= name;
		this.desc = desc;
		
	}

	public void Complete()
	{
		 Debug.printf("complete");
		 Loading.getinstance().connected();
		 CommandFactory.startnext();
	}
	public void Start()
	{
		load();
		
	}
	/**
	 * 加载数据；
	 */
	public void load()
	{
		
		 Loading.getinstance().connecting(desc);
		CommandSyncContainer list = new CommandSyncContainer();

		CommandSyncGetModelDatas csgmd = new CommandSyncGetModelDatas();
		csgmd.setSqlname(name);
		csgmd.putmacro("BCODE",Env.getEnvbcode());
		list.add(csgmd);
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService
				.SendCommandSync(
						list,
						new AsyncCallback<CommandSyncReturnObject>() {

							@Override
							public void onFailure(
									Throwable caught) {
								MessageBox.alert("错误",
										"网络连接失败", null);

							}

							@Override
							public void onSuccess(
									 CommandSyncReturnObject result) {
								if (result.isB()) {
									// BaseStore<BusiObject>
									// salarystore = new
									// BaseStore<BusiObject>();
									if (result
											.getCommandsync() instanceof CommandSyncGetModelDatas) {
//										DeferredCommand.addCommand(new com.google.gwt.user.client.Command(){
//
//											@Override
//											public void execute() {
												store.removeAll();
//												store.add(((CommandSyncGetModelDatas)(result.getCommandsync())).getList(),false);
												store.insert(((CommandSyncGetModelDatas)(result.getCommandsync())).getList(), 0);
												_put();
												Complete();
												
//											}
											
//										});
										
										
									}
								
								} else {
									MessageBox
											.alert(
													"错误",
													"错误未知！"
															+ result
																	.getMessage(),
													null);

								}

							}

				

						});
	
	}
	/**
	 * 加载到BaseStoreList
	 */
	private void _put() {
		BaseStoreList.getInstance().put(name, this);
		
	}
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public BaseStore<BusiObject> getStore() {
		return store;
	}

	public void setStore(BaseStore<BusiObject> store) {
		this.store = store;
	}
	
}
